import useSWR from 'swr';
import { locationOrSkuApi } from '../api/dashboard';
import { useAuthStore2 } from '../store/useAuthStore2';
import { useSpecialStatisticsStore } from '../store/useSpecialStatisticsStore';

export type TableItem = [string, string, string];

/**
 *
 * @param type sku/location 区分
 * @returns
 */
export const useTableData = (type?: string) => {
  const token = useAuthStore2((s) => s.token);
  const { data, error, isLoading } = useSWR(
    () =>
      token
        ? '/report/large-screen/statistics-locationOrSku?page=0&size=1000'
        : null,
    locationOrSkuApi,
  );

  const statisticsStore = useSpecialStatisticsStore.getState();
  //设置数据统计的库存数
  statisticsStore.setInventorySkuPcs(data?.inventorySkuPcs || 0);
  statisticsStore.setInventorySkuSpecs(data?.inventorySkuSpecs || 0);

  const locationVertoryList: TableItem[] = data?.locationVertoryList?.map(
    (item) => [
      item.locationNO || '',
      item.skuCountNum?.toLocaleString() || '',
      item.quantity?.toLocaleString() || '',
    ],
  ) || [['', '', '']];

  const skuVertoryList: TableItem[] = data?.skuVertoryList?.map((item) => [
    item.skuNameCn || '',
    item.inventoryAges || '',
    item.quantity?.toLocaleString() || '',
  ]) || [['', '', '']];

  return {
    listData: { skuVertoryList, locationVertoryList },
    isLoading,
    isError: error,
  };
};
